<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Pager class
 *
 *
 * @author  ZYL <zhangyoulin@gmail.com>
 * @version $Id: pager.class.php, v 1.0  2006/12/11 14:46 danielc Exp $
 * @package Pager
 */


class Pager
{
    /**
     * @var array $source
     */
    var $source;
    
    /**
     * @var int @pagesize
     */
    private $pagesize;
    
    /**
     * @var int $curr
     */
    private $curr;
    
    /**
     * @var int $rows
     */
    private $rows;
    
    /**
     * @var int $pages
     */
    private $pages;
    
    /**
     * @param array $source    
     * @param int   $pagesize  
     */
    public function __construct($source, $pagesize)
    {
        $this->pagesize   = $pagesize;
        $this->source     = $source;
        $this->rows       = count($this->source);
        $this->pages      = (($this->rows) % ($this->pagesize)) ? ((int)(($this->rows) / ($this->pagesize)) + 1) : (($this->rows) / ($this->pagesize));
    }
    
    /**
     * @return array 
     */
    public function getPageContent()
    {
        if ($this->pages == 0) {
            return null;
        }
        
        if (!isset($_GET['page']) || $_GET['page'] == "" || $_GET['page'] < 1) {
            $this->curr = 1;
        } elseif ($_GET['page'] > $this->pages) {
            $this->curr = $this->pages;
        } else {
            $this->curr = $_GET['page'];
        }

        $rowleft  = ($this->curr - 1) * $this->pagesize;
        $rowright = ($rowleft + $this->pagesize < $this->rows) ? ($rowleft + $this->pagesize - 1) : ($this->rows - 1);
        $arr      = range($rowleft, $rowright);
        foreach ($arr as $num) {
            $rowset[] = $this->source[$num];
        }

        return $rowset;	
    }

    /**
     * @return string 
     */
    public function showPager($url = null)
    {
        if (!isset($url)) { 
            $url = $_SERVER["REQUEST_URI"];
        }
        $parse_url = parse_url($url);
        $url_query = isset($parse_url["query"]) ? $parse_url["query"] : null; 

        if ($url_query) {
            $page       = isset($_GET['page']) ? $_GET['page'] : "";
            $url_query  = ereg_replace("(^|&)page=$page", "", $url_query);
            $url_query  = ereg_replace("(^|&)page=", "", $url_query);
            $url        = str_replace($parse_url["query"],$url_query,$url);

            if ($url_query) {
                $url .= "&page"; 
            } else {
                $url .= "page";
            }

        } else {
            $url .= "?page";
        }

        $str   = '';
        $fromP  = ($this->curr - 1) * $this->pagesize + 1;
        $endP   = $this->curr * $this->pagesize ;
        $endP   = $endP < $this->rows  ? $endP : $this->rows ;
        //$str   .= "Total ".$this->pages." pages".$this->rows." entries, this page list ".$fromP."--".$endP." ";

        if ($this->curr == 1 || $this->pages == 0) {
         //   $str .= "<< first |";
         //   $str .= " < previous";
        } else {
            $str .= "<a href='".$url."=1' class='page'> << first </a> | ";
            $str .= "<a href='".$url."=".($this->curr - 1)."'> < previous</a>";  
        }

        if (1 <= $this->curr && $this->curr <= 4) {
            $showMinPage = 1;
            $showMaxPage = ( $this->pages < 9) ? $this->pages : 9;
        } elseif (4 < $this->curr && $this->curr < ($this->pages - 4)) {
            $showMinPage = $this->curr - 4;
            $showMaxPage = $this->curr + 4;
        } elseif (($this->pages - 4) <= $this->curr && $this->curr <= $this->pages) {
            $showMinPage = ($this->pages < 9) ? 1 : ($this->pages - 8);
            $showMaxPage = $this->pages;
        }

        if ($this->pages > 0) {
            $str .= "&nbsp";
            for ($icnt = $showMinPage; $icnt <= $showMaxPage; $icnt++) {
                if ($icnt == $this->curr) {
                    $str .= "<b>".$icnt."</b></a>&nbsp" ;
                } else {
                    $str.= "<a href='".$url."=".$icnt."' >".$icnt."</a>&nbsp" ;
                }
            }
         //   $str .= "&gt;";
        }

        if ($this->curr == $this->pages || $this->pages == 0) {
           // $str.="Next";
        } else {
             $str .= "<a href='".$url."=".($this->curr + 1)."'>next > </a>" . ' | ';
             $str .="<a href='".$url."=".$this->pages."'> last >> </a>";
        }
        //$str .= '  Go to <input type="text" name="page" size="3"> page';
        //$str .= '<input type="submit" value="submit">';
        $str .= "";

        return $str;
    }

    

}
?>